var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');


var app = getApp();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    avatarUrl: defaultAvatarUrl,
    nickname: "",
    showForm: false,
    phoneNumber:"",
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  wxLogin: function (e) {
    let that = this
    if (e.detail.userInfo == undefined) {
      app.globalData.hasLogin = false;
      util.showErrorToast('微信登录失败');
      return;
    }
    if(e.detail.userInfo.nickName == '微信用户'){
      e.detail.userInfo.avatarUrl = ''
      e.detail.userInfo.nickName = ''
    }
    if(e.detail.userInfo.mobile == undefined){
      e.detail.userInfo.mobile = '';
    }
    user.checkLogin().catch(() => {
      wx.showLoading()
      user.loginByWeixin(e.detail.userInfo).then(res => {
        wx.hideLoading()
        if(!res.data.userInfo.avatarUrl || !res.data.userInfo.nickName || res.data.userInfo.nickName == '微信用户'){
          that.setData({
            showForm: true
          })
          app.globalData.hasLogin = false;
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          return false
        }
        app.globalData.hasLogin = true;
        //储存userId
        let spreaderUserId = wx.getStorageSync("spreaderUserId")
        if(spreaderUserId){
          util.request(api.addSpreaderInvite,{spreaderUserId,inviteUserId: res.data.userId},'POST').then(function(res) {
          });
        }
        wx.setStorageSync('userId', res.data.userId);
        wx.setStorageSync('tonke', res.data.token);
        wx.navigateBack({
          delta: 1
        })
      }).catch((err) => {
        wx.hideLoading()
        app.globalData.hasLogin = false;
        util.showErrorToast('微信登录失败');
      });

    });
  },
  accountLogin: function () {
    wx.navigateTo({
      url: "/pages/auth/accountLogin/accountLogin"
    });
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl,
    })
  },
  nicknameChange(e) {
    const {
      value
    } = e.detail
    this.setData({
      nickname: value,
    })
  },
  getPhoneNumber (e) {
    let that = this;
    //请求后端获取解密后的手机号
    util.request(api.bindNoPhone, {
      code: e.detail.code
    }, 'POST').then(function(res) {
      if (res.errno == 0) {
          that.setData({
            phoneNumber:res.data
          });
      }
    });
     
    },
  closeModel() {
    this.setData({
      showForm: false,
    })
  },
  async doLogin(e){
    let that = this
    const nickname = e.detail.value.nickname
    if(this.data.avatarUrl == defaultAvatarUrl){
      util.showErrorToast('请上传头像');
      return false
    }
    if(!nickname){
      util.showErrorToast('请设置您的昵称');
      return false
    }
    if(!that.data.phoneNumber){
      util.showErrorToast('请点击获取手机号');
      return false
    }
    wx.uploadFile({
      url: api.StorageUpload, //仅为示例，非真实的接口地址
      filePath: this.data.avatarUrl,
      name: 'file',
      success (res){
        res.data = JSON.parse(res.data)
        let avatarUrl = res.data.data.url
        that.wxLogin({
          detail: {
            userInfo: {
              avatarUrl,
              nickName: nickname,
              mobile:that.data.phoneNumber
            }
          }
        })
      }
    })
  }
})